/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cbsnrelayparte;

import java.io.*;
import java.net.*;

/**
 *
 * @author Craig Butler (5668670)
 * @author Stephen Norman (616000X)
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception
    {
        // Leave a blank line
        System.out.println();
        
        // Get the server's IP address
        String serverAddress = "";
        
        if (args.length == 2 && args[0].equals("-s"))
            serverAddress = args[1];
        else
        {
            // The user must provide the server's address
            System.out.println("You must provide the server's IP Address in the form: -s xxx.xxx.xxx.xxx");
            System.exit(1);
        }        
        
        // Welcome the user
        System.out.println("HIT2120 CBSN Relay Part E");
        System.out.println("By Craig Butler (616000X) and Stephen Norman (616000X)");
        
        // Leave a blank line
        System.out.println();
        
        // Tell the user about GoBack-N
        System.out.println("This application shows an example GoBack-N protocol in action.");
        System.out.println("We will be taking in packets from the client and forwarding them to the server.");
        System.out.println("These packets may be corrupted, dropped or arrive out of order.");
        
        // Leave a blank line
        System.out.println();
        
        // Create a new Server Socket
        ServerSocket serverSocket = new ServerSocket(4245);
        
        // The relay will run forever, or until the user chooses to close
        // this application
        while (true)
        {
            // Wait for a client to create a connection to us
            Socket clientSocket = serverSocket.accept();
            
            // Open a new connection to the server
            Socket toServerSocket = new Socket(serverAddress, 4246);
            
            // We need to have two different threads,
            // one to read from the client and write to the server
            // and the other one to read from the server and write
            // to the client
            
            // Read from the client and write to the server
            ReaderWriter readClientWriteServer = new ReaderWriter(new DataInputStream(clientSocket.getInputStream()), new DataOutputStream(toServerSocket.getOutputStream()), "client", "server");
            ReaderWriter readServerWriteClient = new ReaderWriter(new DataInputStream(toServerSocket.getInputStream()), new DataOutputStream(clientSocket.getOutputStream()), "server", "client");
            
            // Create the new threads
            Thread rClientWServer = new Thread(readClientWriteServer);
            Thread rServerWClient = new Thread(readServerWriteClient);
            
            // Start the threads
            rClientWServer.start();
            rServerWClient.start();
            
            // We now go back to waiting for more connections
        }
    }

}
